@extends('layouts.app')

@section('content')
    <input id="realLink" type="text" value="{{ $result }}" hidden>
    <div class="row justify-content-center mt-5">
        <div class="col-8">
            <div class="card">
                <div class="card-header">
                    获取下载链接成功
                </div>
                <div class="card-body">
                    <h5 class="card-title" id="filename">{{ $file['server_filename'] }}</h5>
                    <p class="card-text">
                        文件大小：{{ formatSize($file['size']) }}
                    </p>
                    <p class="card-text">
                        文件MD5：{{ $file['md5'] }}
                    </p>
                    <p class="card-text">
                        上传时间：{{ date("Y年m月d日 H:i:s", $file['server_ctime']) }}
                    </p>
                    <hr/>
                    <p>
                        <a href="{{ 'https://' . $result }}">下载地址（浏览器下载需要设置UA，请复制链接到其他工具下载）</a>
                    </p>
                    <p><a id="toMotrix" href="#" data-href="{{ $file['dlink'] }}">发送到Motrix</a></p>
                    <p><a href="https://mp.weixin.qq.com/s/K67RSwNvGhqCuQaAah7osA" target="_blank">下载链接使用帮助（必读）</a></p>
                    <hr/>
                    <p>Tips: 电脑端右键即可复制下载链接，手机端长按可复制下载链接。推荐使用 Aria2、Motrix 下载，使用方法请访问帮助页面。</p>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="aria2ConfigModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">下载</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="prc">RPC地址</label>
                        <input type="text" class="form-control" id="prc" value="ws://localhost:16800/jsonrpc">
                        <small id="prcHelp" class="form-text text-muted">推送aria2默认配置:ws://localhost:6800/jsonrpc</small>
                        <small id="prcHelp" class="form-text text-muted">推送Motrix默认配置:ws://localhost:16800/jsonrpc</small>
                    </div>
                    <div class="form-group">
                        <label for="token">Token</label>
                        <input type="text" class="form-control" id="token" placeholder="没有则留空">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button id="send" type="button" class="btn btn-primary">发送</button>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('script')
<script>
    $(() => {
        let href = null
        $(document).on('click', '#toMotrix', event => {
            href = $(event.currentTarget).data('href')
            $('#aria2ConfigModal').modal('show')
        })
        $(document).on('click', '#send', event => {
            console.log(href)
            let prc = $('#prc').val()
            let link = $('#realLink').val()
            let uris = ['http://' + link, 'https://' + link]

            let pat = /^wss?\:\/\/(((([A-Za-z0-9]+[A-Za-z0-9\-]+[A-Za-z0-9]+)|([A-Za-z0-9]+))(\.([A-Za-z0-9]+[A-Za-z0-9\-]+[A-Za-z0-9]+)|([A-Za-z0-9]+))*(\.[A-Za-z0-9]{2,10}))|localhost|(([01]?\d?\d)|(2[0-4]\d)|(25[0-5]))(\.([01]?\d?\d)|(2[0-4]\d)|(25[0-5])){3}|((\[[A-Za-z0-9:]{2,39}\])|([A-Za-z0-9:]{2,39})))(\:\d{1,5})?(\/.*)?$/;
            if (!pat.test(prc)) {
                alert('PRC地址格式不正确，请重新输入')
                return false
            }
            let ws = new WebSocket(prc)
            let options = {
                "max-connection-per-server": "16",
                "user-agent": "LogStatistic"
            }
            let filename = $('#filename').text()
            if (filename !== '') {
                options.out = filename;
            }
            let json = {
                "id": "baiduwp-php",
                "jsonrpc": '2.0',
                "method": 'aria2.addUri',
                "params": [uris, options],
            }

            ws.onerror = event => {
                console.log(event)
                alert('Aria2 连接错误，请联系管理员')
            };
            ws.onopen = () => {
                ws.send(JSON.stringify(json))
            }
            $('#aria2ConfigModal').modal('hide')
        })
    })
</script>
@endsection
